Publish Your Work

Solving problems in Matlab's command window is very convenient until you wish to report on the work that you have completed. Copying and pasting your Command Window commands and results to a Word Processor is unnecessary thanks to the File -> Publishing options available in Matlab. This lesson shows you how to conveniently save your commands to a Matlab script (an m-file) and then use Matlab to create a PDF that shows your commands and their results in a very user-friendly format. First, you must save the desired commands to an m-file.

1) Save commands to m-file

  1. Use "Ctrl-Left Click" to select individual commands, from the Command History, that solve the problem.
  2. Right-click while the cursor is in the Command History window.
  3. Select Create m-file.
  4. Click the Save file icon to save the file.
  5. Change to the correct directory for your work.
  6. Enter a valid and descriptive name for your file.
  7. Run your Matlab script. There are several ways to do this:
    • Select Debug->Run from the Editor window menu.
    • Click the Save and Run icon to save and run the script.
    • Type the name of the m-file in the Command Window and press Enter.
    • Type the F5 key.
  8. Fix any syntax or runtime errors that occur.
commands in the history window and right-click save to m-file

2) Create Cell Divisions

A cell is a logical block of code. Each cell has a header, a comment block, and the code, that solve that part of the problem. You must decide where the cell divisions must be. A good start is to have one cell for each part of a problem's solution.

  • Select Cell -> Enable Cell Mode from the Editor menu to turn on cell syntax highlighting.
  • Add a cell header for each cell that you identify. The cell's header starts with %%. It must be followed by a blank space and then a descriptive title for that part of the solution.
  • Use the (%) to add a regular comment that describes the cell's purpose in the larger context of the problem's solution.
  • Edit and add code as needed to complete the work.
  • Save and Run your script (m-file).

You may also include equations in the first comment section of any cell in your m-files. All capabilities can't be shown here, but the basic idea is to put your equation in a comment block with blank lines before and after and the characters $$ at the start and end of the equation.

The commentswill produce
	%
	% $$y = \frac{1}{x^2}$$
	%
	
y equals x squared

Notice, how you must enter fractions using:

\frac{numerator}{denominator}

The blank lines before and after the equations are required.

Caution: Comment lines that describe equations must be in the first comment block in the cell. If the comment lines come after any MATLAB code statements in the given cell, they will be listed as is in the code and will not be translated into the equation form we are showing here.

add cell divisions

3) Publish to HTML

Just saving the commands in an m-file is a big step. You can easily print the contents of an m-file and you can edit, rerun, and retry your solution in many variations. However, the results of each command are not shown in an m-file and the text-only format is not as nice as a more polished document can show. A published document is better for handing in work for homework assignments and for presenting your work to a co-worker or Supervisor. To publish your solution to an easy to read (and print) HTML file:

  1. Be sure that the editor window for your m-File is active (highlighted).
  2. Select File -> Publish from the Menu bar.

This will save your file, run your file, create a web page and open the new HTML file in a Web Browser. The name of the html file will be the name of your script.html and it will be saved in a folder named html in the same folder as your script. For CS310 homeworks, print out this file and handin as directed on the homework.

final html result

4) Publish to PDF

By default, publishing in MATLAB creates an HTML page and supporting files. However, Portable Document Format (pdf) is more convenient for many users and for uploading to Learn@UW dropboxes. To publish your work to PDF, you must configure the publish tool. Save both configuration options and then you will be able to choose which publishing option you wish to perform for this program script.

To add an option for publishing your solution to a PDF file:

  1. Be sure that the editor window for your m-File is active (highlighted).
  2. Move your cursor over File -> Publish Configuration for script.m -> Edit Publish Configurations for file.m from the Menu bar.
  3. In the Edit Configurations dialog box, click the Add Configuration button and select Publish Configuration.
  4. Change the name of the configuration to pdf.
  5. Change the Output file format to pdf.
  6. Click Close to save your newly added configuration.

When completed, your Edit Configuration dialog should look like this.

Edit Configuration Screen

The new configuration can be used to create PDF versions of your work.

5) To change your default Publishing Format and other options:

  1. Be sure that the editor window for your m-File is active (highlighted).
  2. Move your cursor over File -> Publish Configuration for script.m -> Edit Publish Configurations for file.m from the Menu bar.
  3. In the Edit Configurations dialog box, select User Default from the Publish Settings dropdown menu.
  4. Change all configuration settings fields as you desire.
  5. Click the Save As... button.
  6. Select User Default
  7. Click Overwrite to save the new settings as your default.

6) To Publish a function definition m-file:

Functions require input values to execute. Thus to publish a function definition, it is necessary to edit the Publish Configurations for the function definition with a some sample input values.

To change the publishing options for your Function:

  1. Be sure that the editor window for your function's m-File is active (highlighted).
  2. Move your cursor over File -> Publish Configuration for script.m -> Edit Publish Configurations for file.m from the Menu bar.
  3. In the Edit Configurations dialog box, modify the MATLAB expression field so that it shows a valid call for your function. Add paretheses and and valid test argument values after the name of your function.
    For example:
                x = 10, y = -2, name = 'Deb'
                fname( x, y, name );
  4. Edit other Publish settings as needed.
  5. Click the Close button to save changes.
  6. Click Publish to save the new settings as your default.
Configuration Screen for publishing functions

7) Include link to a function defintion

To include a link to a function definition from the program or function that calls that function, include a cell with the following information: This example assumes that you have defined a function named MB that accepts three input parameters, (k,T,E). It also assumes that the function definition file is in the same directory as the script that is being published to HTML.

%% Part 3.b Define MB(k,T,E) to compute Maxwell-Boltzmanm
% See <../MB.m MB.m>
    

See the online help for the publish command for more publishing options.

image of link to a function definition